package com.admission.service;

import java.io.File;
import java.util.List;

import org.apache.struts2.ServletActionContext;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.admission.dao.EnvironmentPictureDao;
import com.admission.model.Environment;
import com.admission.model.EnvironmentPicture;
import com.admission.util.DeleteFile;
@Repository
@Transactional
public class EnvironmentPictureManager {
	private EnvironmentPictureDao environmentPictureDao;
	private EnvironmentManager environmentManager;
	
	/**
	 * 保存
	 * @param entity
	 */
	public void save(EnvironmentPicture entity){
		environmentPictureDao.save(entity);
	}
	
	/**
	 * 通过id查找位置对象，无返回null
	 * 
	 * @param id
	 * @return
	 */
	public EnvironmentPicture getById(String id){
		return environmentPictureDao.findUniqueBy("id", id);
	}
	
	/**
	 * 保存或更新图片
	 * 
	 * @param adId
	 * @param main
	 * @param sub
	 */
	public void save(String EnId, String[] sub,String[] title) {
//		String serverrealPath = ServletActionContext.getServletContext()
//				.getRealPath("/upload");
//		if(sub!=null){
//		List<EnvironmentPicture> l = environmentPictureDao.findBy("environment.id", EnId);
//		for (EnvironmentPicture picture : l) {
//			File file = new File(serverrealPath + File.separator
//					+ picture.getUrl());
//			DeleteFile.deleteByFile(file);
//			environmentPictureDao.delete(picture);
//		}
		Environment a = environmentManager.getById(EnId);
		if (sub != null) {
			for (int i = 0; i < sub.length; i++) {
				EnvironmentPicture p = new EnvironmentPicture();
				p.setUrl(sub[i]);
				p.setEnvironment(a);
				p.setTitle(title[i]);
				environmentPictureDao.save(p);
			}
		}}
	

	/**
	 * 
	 * 根据Environment id批量删除
	 * @param ids
	 */
	public void deleteByIds(String id) {
		String serverrealPath = ServletActionContext.getServletContext()
				.getRealPath("/upload");
			List<EnvironmentPicture> p = environmentPictureDao.findBy("environment.id", id);
		for (EnvironmentPicture picture : p) {
			File file = new File(serverrealPath + File.separator
					+ picture.getUrl());
			DeleteFile.deleteByFile(file);
			environmentPictureDao.delete(picture);
		}
	}
	
	/**
	 * 删除单张图片
	 * @param id
	 */
	public void deleteById(String id){
		String serverrealPath = ServletActionContext.getServletContext()
				.getRealPath("/upload");
		EnvironmentPicture picture=environmentPictureDao.findUniqueBy("id", id);
			File file = new File(serverrealPath + File.separator
					+ picture.getUrl());
			DeleteFile.deleteByFile(file);
			environmentPictureDao.delete(picture);
	
	}
	
	
	/**
	 * 根据Environment id 获取图片集合
	 * 
	 * @param id
	 * @return
	 */
	public List<EnvironmentPicture> getSubPicture(String id) {
		Criterion criterions = Restrictions.eq("environment.id", id);
		return environmentPictureDao.find(criterions);
	}
	
	public EnvironmentPictureDao getEnvironmentPictureDao() {
		return environmentPictureDao;
	}
	
	@Autowired
	public void setEnvironmentPictureDao(EnvironmentPictureDao environmentPictureDao) {
		this.environmentPictureDao = environmentPictureDao;
	}

	public EnvironmentManager getEnvironmentManager() {
		return environmentManager;
	}
	
	@Autowired
	public void setEnvironmentManager(EnvironmentManager environmentManager) {
		this.environmentManager = environmentManager;
	}
}
